{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 模拟http请求\n",
    "\n",
    "import asyncio\n",
    "import time\n",
    "from urllib.parse import urlparse\n",
    "\n",
    "async def get_url(url):\n",
    "    url = urlparse(url)\n",
    "    host = url.netloc\n",
    "    path = url.path\n",
    "    if path == \"\":\n",
    "        path = \"/\"\n",
    "\n",
    "    reader, writer = await asyncio.open_connection(host, 80)\n",
    "    writer.write(\"GET {} HTTP/1.1\\r\\nHost:{}\\r\\nConnection:close\\r\\n\\r\\n\".format(host, 80).encode(\"utf-8\"))\n",
    "    all_lines = []\n",
    "    async for raw_line in reader:\n",
    "        data = raw_line.decode(\"utf-8\")\n",
    "        all_lines.append(data)\n",
    "    html = \"\\n\".join(all_lines)\n",
    "    return html\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    start_time = time.time()\n",
    "    loop = asyncio.get_event_loop()\n",
    "    tasks = []\n",
    "    for url in range(20):\n",
    "        url = \"http://shop.projectsedu.com/goods/{}/\".format(url)\n",
    "        tasks.append(asyncio.ensure_future(get_url(url)))\n",
    "\n",
    "    loop.run_until_complete(asyncio.wait(tasks))\n",
    "\n",
    "    for task in tasks:\n",
    "        print(task.result())\n",
    "    print(\"last time: {}\".format(time.time() - start_time))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "## 事件循环封装\n",
    "\n",
    "import asyncio\n",
    "import time\n",
    "from urllib.parse import urlparse\n",
    "\n",
    "async def get_url(url):\n",
    "    url = urlparse(url)\n",
    "    host = url.netloc\n",
    "    path = url.path\n",
    "    if path == \"\":\n",
    "        path = \"/\"\n",
    "\n",
    "    # client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)\n",
    "    # client.connect((host, 80))\n",
    "    # client.send(\"GET {} HTTP/1.1\\r\\nHost:{}\\r\\nConnection:close\\r\\n\\r\\n\".format(host, path).encode(\"utf-8\"))\n",
    "\n",
    "    reader, writer = await asyncio.open_connection(host, 80)\n",
    "    writer.write(\"GET {} HTTP/1.1\\r\\nHost:{}\\r\\nConnection:close\\r\\n\\r\\n\".format(host, 80).encode(\"utf-8\"))\n",
    "    all_lines = []\n",
    "    async for raw_line in reader:\n",
    "        data = raw_line.decode(\"utf-8\")\n",
    "        all_lines.append(data)\n",
    "    html = \"\\n\".join(all_lines)\n",
    "    return html\n",
    "\n",
    "async def main():\n",
    "    tasks = []\n",
    "    for url in range(20):\n",
    "        url = \"http://shop.projectsedu.com/goods/{}/\".format(url)\n",
    "        tasks.append(asyncio.ensure_future(get_url(url)))\n",
    "    for task in asyncio.as_completed(tasks):\n",
    "        result = await task\n",
    "        print(result)\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    start_time = time.time()\n",
    "    loop = asyncio.get_event_loop()\n",
    "\n",
    "    loop.run_until_complete(main())\n",
    "    print(\"last time: {}\".format(time.time() - start_time))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
